<?php
namespace common\models;


class TradeGrade extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'trade_grade';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['grade', 'name'], 'required'],
            [['grade'], 'integer'],
            [['name', 'en_name'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'grade' => 'Grade',
            'name' => 'Name',
            'en_name' => '英文',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();
		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		foreach($extends as $extend)
		{

		}

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

	## 获取加速释放配置
	public static function getSpeedBonusConf($code, $modelId = '')
	{
		$where = ['model_code'=>$code];		
		if($modelId) $where['a.model_id'] = $modelId;
		
		$data  = TradeSpeedBonus::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_model as b','a.model_id = b.model_id')
					->where($where)
					->orderBy('level asc')
					->asarray()->all();
		return $data;	
	}

	## 会员等级
	public static function gradeConf($lang = 'cn')
	{
		$data = self::find()->asarray()->all();
		if(!$data) return false;
		$list = array();
		foreach($data as $val)
		{
			$list[$val['grade']] = $lang=='en' ? $val['en_name'] : $val['name'];
		}
		return $list;
	}	


}